$(
    '<tr valign="middle" align="center" height="20">'   +
        '<td class="cell1"></td>'         +
        '<td class="cell1"><b>Gesamtkosten Multiplex</b>:</td>'         +
        '<td class="cell1"><div id="collector_costs_m">0</div></td>' +
        '</tr>' +
        '<tr valign="middle" align="center" height="20">'   +
        '<td class="cell"></td>'         +
        '<td class="cell"><b>Gesamtkosten Dyharra</b>:</td>'         +
        '<td class="cell"><div id="collector_costs_d">0</div></td>' +
        '</tr>' +
        '<tr valign="middle" align="center" height="20">' +
        '<td class="cell1"></td>' +
        '<td class="cell1"><b>Punkte</b>:</td>' +
        '<td class="cell1"><div id="collector_points">0</div></td>' +
        '</tr>'
).insertAfter('tr:contains("Kollektorbau"):last');


// append listener to calc the points / costs
$('input[name^="b_col"]').keyup(function() {
    var collectors  = parseInt(getCurrentCollectors());
    var toBuild     = parseInt($('input[name^="b_col"]').val());

    calcCollectorPoints(collectors, toBuild);
    calcCollectorCosts(collectors, toBuild);
});

getCurrentCollectors = function() {
    var t   = $('td:contains("im Bau"):last').text();
    var num = parseInt(t.substring(0, t.indexOf(" (")));

    // we also need to take in account the collectors already queued to build
    num += parseInt(t.substring(t.indexOf("(")+1, t.indexOf(" im Bau")));

    return num;
}

/** calc the collectors points */
calcCollectorPoints = function(collectors, toBuild) {
    var points = 0;

    if(collectors >= 1000) {
        points = (toBuild * 20000);
    }
    else if((collectors + toBuild) < 100) {
        points = (toBuild * 10000);
    }
    else {
        for(var i=1; i<=(collectors + toBuild); i++) {
            if(i > collectors) {
                points += pointsForCollectors(i);
            }
        }
    }

    $('#collector_points').text($.ewigen.util.int2pointint(points));
}

pointsForCollectors = function(i) {
    if(i<100) {
        return 10000;
    }
    else if(i >= 100 && i < 150) {
        return 10500;
    }
    else if(i >= 150 && i < 200) {
        return 11000;
    }
    else if(i >= 200 && i < 250) {
        return 11500;
    }
    else if(i >= 250 && i < 300) {
        return 12000;
    }
    else if(i >= 300 && i < 350) {
        return 12500;
    }
    else if(i >= 350 && i < 400) {
        return 13000;
    }
    else if(i >= 400 && i < 450) {
        return 13500;
    }
    else if(i >= 450 && i < 500) {
        return 14000;
    }
    else if(i >= 500 && i < 550) {
        return 14500;
    }
    else if(i >= 550 && i < 600) {
        return 15000;
    }
    else if(i >= 600 && i < 650) {
        return 15500;
    }
    else if(i >= 650 && i < 700) {
        return 16000;
    }
    else if(i >= 700 && i < 750) {
        return 16500;
    }
    else if(i >= 750 && i < 800) {
        return 17000;
    }
    else if(i >= 800 && i < 850) {
        return 17500;
    }
    else if(i >= 850 && i < 900) {
        return 18000;
    }
    else if(i >= 900 && i < 950) {
        return 18500;
    }
    else if(i >= 950 && i < 1000) {
        return 19000;
    }
    else {
        return 20000;
    }
    return 0;
}

/** calc the costs for the entered collectors */
calcCollectorCosts = function(collectors, toBuild) {
    //7,5n²-15n+1007,5 Multiplex und n²-2n+101 Dyharra
    var count = toBuild;
    var costM = 0, costD = 0;
    
    for(var i=collectors+1; i<=(collectors + count); i++) {
        costM += parseInt((7.5 * (i*i)) - (15*i) + 1007.5);
        costD += parseInt((i*i) - (2*i) + 101);
    }

    $('#collector_costs_m').text($.ewigen.util.int2pointint(costM));
    $('#collector_costs_d').text($.ewigen.util.int2pointint(costD));
}



